<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/5 0005
 * Time: 09:35
 */

namespace app\admin\controller;


use think\Db;

class User extends Base
{
    public $table = 'users';
    public function index()
    {
        $keyword = input('keyword');
        $verify_status = input('verify_status');
        $role = input('role');
        $verify_status = $verify_status == ''?9:$verify_status;

        $where = null;
        if($keyword){
            $where['name|email'] = ['like','%'.$keyword.'%'];
        }
        if($role){
            $where['role'] = ['eq',$role];
        }
        if($verify_status != '' && $verify_status != 9)
            $where['verify_status'] = ['eq',$verify_status];
        if(!$where){
            $where = '1=1';
        }
        $data = Db::name($this->table)
            ->where($where)
            ->order('id desc')
            ->paginate();
        $page = $data->render();
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('now',time());
        $this->assign('keyword',$keyword);
        $this->assign('verify_status',$verify_status);
        $this->assign('role',$role);
        return $this->fetch();
    }

    public function verify()
    {
        $id = input('id');
        $user = $this->getUser($id);
        if($user['verify_status']!= 1){
            $this->error('不能审核');
        }

        switch ($user['role']){
            case 1:
                $data = Db::name('exhibitors')->where('user_id',$id)->find();
                $data['booth'] = Db::name('booths')->where('user_id',$id)->find();
                break;
            case 2:
                $data = Db::name('visitors')->where('user_id',$id)->find();
                break;
            case 3:
                $data = Db::name('reporters')->where('user_id',$id)->select();
                if($data){
                    foreach ($data as &$value){
                        $value['paperwork_url'] = $this->fileUrl($value['paperwork_url']);
                        $value['idcard_url'] = $this->fileUrl($value['idcard_url']);
                    }
                }
                break;
            default:
                $data = null;
        }
        if(!isset($data['booth']))
            $data['booth'] = null;
        if($user['role'] <3){
            $data['producer_type'] = $this->getConfig($data['producer_type'],'producer_types');
            $data['publisher_type'] = $this->getConfig($data['publisher_type'],'publisher_types');
            $data['purchaser_type'] = $this->getConfig($data['purchaser_type'],'purchaser_types');
            $data['purchase_range'] = $this->getConfig($data['purchase_range'],'purchase_ranges');
            $data['other_type'] = $this->getConfig($data['other_type'],'other_types');
            $data['stuff'] = Db::name('staff')->where('user_id',$id)->select();
            if($data['stuff']){
                foreach ($data['stuff'] as &$v){
                    $v['avatar_url'] = $this->fileUrl($v['avatar_url']);
                }
            }
        }


        $user['credential_url'] = $this->fileUrl($user['credential_url']);

        $this->assign('data',$data);
        $this->assign('user',$user);
        return $this->fetch();
    }

    public function info()
    {
        $id = input('id');
        $user = $this->getUser($id);

        switch ($user['role']){
            case 1:
                $data = Db::name('exhibitors')->where('user_id',$id)->find();
                $data['booth'] = Db::name('booths')->where('user_id',$id)->find();
                break;
            case 2:
                $data = Db::name('visitors')->where('user_id',$id)->find();
                break;
            case 3:
                $data = Db::name('reporters')->where('user_id',$id)->select();
                if($data){
                    foreach ($data as &$value){
                        $value['paperwork_url'] = $this->fileUrl($value['paperwork_url']);
                        $value['idcard_url'] = $this->fileUrl($value['idcard_url']);
                    }
                }
                break;
            default:
                $data = null;
        }
        if(!isset($data['booth']))
            $data['booth'] = null;
        if($user['role'] <3){
            $data['producer_type'] = $this->getConfig($data['producer_type'],'producer_types');
            $data['publisher_type'] = $this->getConfig($data['publisher_type'],'publisher_types');
            $data['purchaser_type'] = $this->getConfig($data['purchaser_type'],'purchaser_types');
            $data['purchase_range'] = $this->getConfig($data['purchase_range'],'purchase_ranges');
            $data['other_type'] = $this->getConfig($data['other_type'],'other_types');
            $data['stuff'] = Db::name('staff')->where('user_id',$id)->select();
            if($data['stuff']){
                foreach ($data['stuff'] as &$v){
                    $v['avatar_url'] = $this->fileUrl($v['avatar_url']);
                }
            }
        }


        $user['credential_url'] = $this->fileUrl($user['credential_url']);

        $this->assign('data',$data);
        $this->assign('user',$user);
        return $this->fetch();
    }


    public function getConfig($data,$type)
    {
        if(!$data)
            return '';
        $config = config('app.'.$type);
        $data = json_decode($data,1);
        $back = [];
        foreach ($data as $value){
            $back[] = isset($config[$value])?$config[$value]:'';
        }
        return join(',',$back);
    }

    public function getUser($id)
    {
        $user =  Db::name($this->table)->where('id',$id)->find();
        if(!$user)
            $this->error('不存在');
        return $user;
    }

    /**
     * 审核通过
     */
    public function pass()
    {
        $id = input('id');
        return $this->basePass($id,3);

    }

    /**
     * 审核不通过
     */
    public function unpass()
    {
        $id = input('id');
        $reason = input('reason');
        return $this->basePass($id,2,$reason);
    }

    protected function basePass($id,$status,$reason = '')
    {
        $user = $this->getUser($id);
        if($user['verify_status'] != 1){
            return dataReturn(0,'不能进行审核');
        }
        $data['verification_detail'] = $reason;
        $data['verify_status'] = $status;
        $res = Db::name('users')->where('id',$id)->where('verify_status',1)->update($data);
        if($res)
            return dataReturn(1,'保存成功');
        return dataReturn(0,'保存失败');
    }



}